﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Web.TableHtmlGeterate
{
    /// <summary>
    /// 创建表格的帮助类
    /// </summary>
    public class TableBulider
    {
        /// <summary>
        /// 生成关联期货表格内容
        /// </summary>
        /// <param name="type">关联期货的数据集合</param>
        /// <returns>前台HTML源码</returns>
        private static string BuildIndexFuturesTBody(List<IndexFutures> future)
        {
            StringBuilder str = new StringBuilder();
            if (future != null && future.Count > 0)
            {
                foreach (var item in future)
                {
                    str.Append("<tr>");
                    str.AppendFormat("<td>{0}</td>", item.NAME);
                    str.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}</font></span></td>", item.NEW, Common.Tools.GetUpDownCss(item.PXCHG, 0));
                    str.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}</font></span></td>", item.PXCHG, Common.Tools.GetUpDownCss(item.PXCHG, 0));
                    str.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}%</font></span></td>", item.Change, Common.Tools.GetUpDownCss(item.PXCHG, 0));
                    str.AppendFormat("<td><span class=\"tr\">{0}</span></td>", item.HOLD);
                    str.Append("</tr>");
                }
            }
            else
            {
                str.Append("<tr>");
                str.Append("<td colspan='5'>暂无数据</td>");
                str.Append("</tr>");
            }
            return str.ToString();
        }

        /// <summary>
        /// 生成关联期货表格内容
        /// </summary>
        /// <param name="type">关联期货的数据集合</param>
        /// <returns>前台HTML源码</returns>
        public static string BuildIndexFuturesTBody(string[] indexFuturesCode)
        {
            List<IndexFutures> list = DataCapture.GetIndexFuturesDatasInCache(indexFuturesCode);
            return BuildIndexFuturesTBody(list);
        }

        ///// <summary>
        ///// 生成近期数据的表格
        ///// </summary>
        ///// <param name="token">一共查询六天的数据集合</param>
        ///// <returns>前台HTML源码</returns>
        //private static string BuildCurrentDataTBody(List<App_Codes.Prices> datas, List<string> tableColumns)
        //{
        //    //表头
        //    StringBuilder strHead = new StringBuilder();
        //    //表体
        //    StringBuilder strBody = new StringBuilder();

        //    int _nowIndex = 0;


        //    if (datas != null && datas.Count() > 0)
        //    {
        //        strHead.Append("<tr>");
        //        strHead.Append("<th width=\"17%\">日期</th>");

        //        for (int i = 1; i < 6; i++)
        //        {
        //            switch (_nowIndex)
        //            {
        //                case 0:
        //                    strHead.AppendFormat("<th width=\"16%\">{0}</th>", datas[i].EDATE);
        //                    break;
        //                case 1:
        //                    strHead.AppendFormat("<th width=\"16%\">{0}</th>", datas[i].EDATE);
        //                    break;
        //                case 2:
        //                    strHead.AppendFormat("<th width=\"14%\">{0}</th>", datas[i].EDATE);
        //                    break;
        //                case 3:
        //                    strHead.AppendFormat("<th width=\"18%\">{0}</th>", datas[i].EDATE);
        //                    break;
        //                case 4:
        //                    strHead.AppendFormat("<th width=\"19%\">{0}</th>", datas[i].EDATE);
        //                    break;
        //                default:
        //                    break;
        //            }

        //        }

        //        for (int j = 0; j < tableColumns.Count; j++)
        //        {
        //            strBody.Append("<tr>");
        //            strBody.AppendFormat("<td>" + tableColumns[0] + "</td>");

        //            for (int i = 1; i < 6; i++)
        //            {
        //                strBody.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}</font></span></td>", datas[j].PriceList[i], Common.Tools.GetUpDownCss(datas[j].PriceList[i], datas[j - 1].PriceList[i]));
        //            }

        //            strBody.Append("</tr>");
        //        }

        //        strHead.Append("</tr>");
        //        _nowIndex = 0;
        //    }
        //    else
        //    {
        //        strHead.Append("<tr>");
        //        strHead.Append("<td colspan='6'>暂无数据</td>");
        //        strHead.Append("</tr>");
        //    }

        //    strHead.Append(strBody.ToString());
        //    return strHead.ToString();
        //}

        ///// <summary>
        ///// 生成近期数据的表格
        ///// </summary>
        ///// <param name="tableName"></param>
        ///// <param name="code"></param>
        ///// <param name="queryCount"></param>
        ///// <param name="orderBy"></param>
        ///// <returns></returns>
        //public static string BuildCurrentDataTBody(string tableName, string filters, string orderBy, List<string> tableColumns, string priceColume)
        //{
        //    List<string> priceColumns = new List<string>();
        //    priceColumns.Add(priceColume);
        //    List<App_Codes.Prices> list = DataCapture.GetCurrentDatas(tableName, filters, orderBy, priceColumns);
        //    return BuildCurrentDataTBody(list, tableColumns);
        //}

        /// <summary>
        /// 生成关联行业的表格
        /// </summary>
        /// <param name="industryIndexs">关联行业的数据集合</param>
        /// <returns>前台HTML源码</returns>
        private static string BuildRelatedIndustriesTBody(List<Com.Stockstar.Quote.Model.Quote.IndustryIndex> industryIndexs)
        {
            StringBuilder str = new StringBuilder();

            if (industryIndexs != null && industryIndexs.Count > 0)
            {
                foreach (var item in industryIndexs)
                {
                    if (item.Name.Equals("电力、煤气及水的生产和供应业"))
                    {
                        item.Name = "水电煤";
                    }
                    if (item.Name.Equals("农、林、牧、渔业"))
                    {
                        item.Name = "农林牧渔业";
                    }
                    str.Append("<tr>");
                    str.AppendFormat("<td><a href=\"http://quote.stockstar.com/stock/industry_{1}.shtml\" target=\"_blank\">{0}</a></td>", item.Name, item.Code);
                    str.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}%</font></span></td>", item.UpDownRate, Common.Tools.GetUpDownCss(Convert.ToDouble(item.UpDownRate), 0));
                    str.AppendFormat("<td><span class=\"tr\">{0:N2}</span></td>", (item.StockAMarketValue / 100000000));
                    str.AppendFormat("<td><span class=\"tr\">{0:N2}</span></td>", (item.Volume / 1000000));
                    str.Append("</tr>");
                }
            }
            else
            {
                str.Append("<tr>");
                str.Append("<td colspan='4'>暂无数据</td>");
                str.Append("</tr>");
            }

            return str.ToString();
        }

        /// <summary>
        /// 生成关联行业的表格
        /// </summary>
        /// <param name="industryCode">行业代码</param>
        /// <returns>前台HTML源码</returns>
        public static string BuildRelatedIndustriesTBody(string industryCode)
        {
            List<Com.Stockstar.Quote.Model.Quote.IndustryIndex> list = DataCapture.GetIndustryIndexDatasInCache(industryCode);
            return BuildRelatedIndustriesTBody(list);
        }

        /// <summary>
        /// 生成同行业个股一览的表格
        /// </summary>
        /// <param name="quoteSnaps">行情快照的数据集合</param>
        /// <returns>前台HTML源码</returns>
        private static string BuildTheSameIndustryStocksTBody(List<Com.Stockstar.Quote.Model.Quote.QuoteSnap> quoteSnaps)
        {
            StringBuilder str = new StringBuilder();
            if (quoteSnaps != null && quoteSnaps.Count > 0)
            {
                foreach (var item in quoteSnaps)
                {
                    str.Append("<tr>");
                    str.AppendFormat("<td><a href=\"http://stock.quote.stockstar.com/{1}.shtml\" target=\"_blank\">{0}</a></td>", item.Name, item.Code);
                    str.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}</font></span></td>", item.CurrentPrice, Common.Tools.GetUpDownCss(Convert.ToDouble(item.UpDownValue), 0));
                    str.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}</font></span></td>", item.UpDownValue, Common.Tools.GetUpDownCss(Convert.ToDouble(item.UpDownValue), 0));
                    str.AppendFormat("<td><span class=\"tr\"><font class=\"{1}\">{0:N2}%</font></span></td>", item.UpDownRate * 100, Common.Tools.GetUpDownCss(Convert.ToDouble(item.UpDownRate), 0));
                    str.Append("</tr>");
                }
            }
            else
            {
                str.Append("<tr>");
                str.Append("<td colspan='4'>暂无数据</td>");
                str.Append("</tr>");
            }
            return str.ToString();
        }

        /// <summary>
        /// 生成同行业个股一览的表格
        /// </summary>
        /// <param name="industryCode">行业代码</param>
        /// <returns>前台HTML源码</returns>
        public static string BuildTheSameIndustryStocksTBody(string industryCode)
        {
            List<Com.Stockstar.Quote.Model.Quote.QuoteSnap> list = DataCapture.GetQuoteSnapDatas(industryCode);
            return BuildTheSameIndustryStocksTBody(list);
        }

        //public static List<App_Codes.Prices> GetSimpleDataFromSixDatas()
        //{

        //}


        public static string GetEDATAString(DateTime edate, string pageCode)
        {
            if (pageCode == "GFJQ" || pageCode == "YM" || pageCode == "DD" || pageCode == "ZXD" || pageCode == "WXD" || pageCode == "YD" || pageCode == "HXM" || pageCode == "BXM" || pageCode == "HHM" || pageCode == "QSHYD" || pageCode == "GHYYDL" || pageCode == "CXJMSHYD" || pageCode == "DXYW" || pageCode == "YZYW" || pageCode == "TXYW" || pageCode == "GYSYYH" || pageCode == "GFZSYYH" || pageCode == "CSSYYH" || pageCode == "YHYJRJG" || pageCode == "QTLJRJG")
            {
                return edate.Year + "年" + edate.Month + "月";
            }
            if (pageCode == "NLMY")
            {
                return edate.Year + "年" + edate.Month + "季度";
            }
            else
            {
                return edate.ToShortDateString();
            }

        }

        private static int GetColorType(string pageCode)
        {

            int tongbi = 0;
            if (pageCode == "GYSYYH" || pageCode == "GFZSYYH" || pageCode == "CSSYYH" || pageCode == "YHYJRJG" || pageCode == "QTLJRJG")
            {
                tongbi = 1;
            }
            if (pageCode == "DXYW" || pageCode == "YZYW" || pageCode == "TXYW" || pageCode == "QSHYD" || pageCode == "GHYYDL" || pageCode == "CXJMSHYD")
            {
                tongbi = 2;
            }
            if (pageCode == "ZZSSJG" || pageCode == "SPFJYSJ")
            {
                tongbi = 3;
            }
            return tongbi;

        }


        /// <summary>
        /// 生成近期数据的表格(价格上限，价格下限的表)
        /// </summary>
        /// <param name="token">一共查询六天的数据集合</param>
        /// <returns>前台HTML源码</returns>
        private static string BuildCurrentDataOfUpDownTBody(List<App_Codes.Prices> datas, List<string> tableColumns, string pageCode)
        {
            //表头
            StringBuilder strHead = new StringBuilder();
            //表体
            StringBuilder strBody = new StringBuilder();


            if (datas != null && datas.Count() > 0)
            {
                strHead.Append("<tr>");
                strHead.Append("<th width=\"17%\">日期</th>");

                for (int i = 1; i < 6; i++)
                {
                    strHead.AppendFormat("<th id=\"r0c{1}\">{0}</th>", GetEDATAString(datas[i].EDATE, pageCode), i - 1);
                }

                for (int j = 0; j < tableColumns.Count; j++)
                {

                    strBody.Append("<tr>");
                    strBody.AppendFormat("<td style=\"text-align:left;\" id=\"rowname" + j + "\">" + tableColumns[j] + "</td>");

                    for (int i = 1; i < 6; i++)
                    {

                        //    strBody.AppendFormat("<td><span class=\"tr\" id=\"r" + (j + 1) + "c" + (i - 1) + "\"><font class=\"{1}\">{0:N2}</font></span></td>", datas[i].PriceList[j], Common.Tools.GetUpDownCss(datas[i].PriceList[0], datas[i - 1].PriceList[0]));
                        int colorType = GetColorType(pageCode);
                        if (colorType == 0)
                        {
                            strBody.AppendFormat("<td><span class=\"tr\" id=\"r" + (j + 1) + "c" + (i - 1) + "\"><font class=\"{1}\">{0:N2}</font></span></td>", datas[i].PriceList[j], Common.Tools.GetUpDownCss(datas[i].PriceList[0], datas[i - 1].PriceList[0]));
                        }
                        else if (colorType == 1 || colorType == 2)
                        {
                            if (j == 0)
                            {
                                strBody.AppendFormat("<td><span class=\"tr\" id=\"r" + (j + 1) + "c" + (i - 1) + "\"><font class=\"{1}\">{0:N2}</font></span></td>", datas[i].PriceList[j], Common.Tools.GetUpDownCss(datas[i].PriceList[0], datas[i - 1].PriceList[0]));
                            }
                            else
                            {
                                strBody.AppendFormat("<td><span class=\"tr\" id=\"r" + (j + 1) + "c" + (i - 1) + "\">{0:N2}</span></td>", datas[i].PriceList[j]);
                            }
                        }
                        else
                        {

                            strBody.AppendFormat("<td><span class=\"tr\" id=\"r" + (j + 1) + "c" + (i - 1) + "\">{0:N2}</span></td>", datas[i].PriceList[j]);

                        }
                    }

                    strBody.Append("</tr>");
                }

                strHead.Append("</tr>");

            }
            else
            {
                strHead.Append("<tr>");
                strHead.Append("<td colspan='6'>暂无数据</td>");
                strHead.Append("</tr>");
            }

            strHead.Append(strBody.ToString());
            return strHead.ToString();
        }

        public bool NeedChange(string pageCode)
        {
            bool needchange = false;
            if (pageCode == "NLMY" || pageCode == "DXYW" || pageCode == "YZYW" || pageCode == "TXYW" || pageCode == "GYSYYH" || pageCode == "GFZSYYH" || pageCode == "CSSYYH" || pageCode == "YHYJRJG" || pageCode == "QTLJRJG")
            {
                needchange = true;
            }
            else if (pageCode == "YPJJGP" || pageCode == "WQQY" || pageCode == "JKMY" || pageCode == "CY" || pageCode == "RLY380" || pageCode == "RLY180" || pageCode == "SNY")
            {
                needchange = true;
            }
            else if (pageCode == "YM" || pageCode == "DD" || pageCode == "ZXD" || pageCode == "WXD" || pageCode == "YD" || pageCode == "HXM" || pageCode == "BXM" || pageCode == "HHM")
            {
                needchange = true;
            }
            else if (pageCode == "ABJC" || pageCode == "SALC" || pageCode == "ALC")
            {
                needchange = true;
            }
            else if (pageCode == "DTYH" || pageCode == "SXHY" || pageCode == "PTHM" || pageCode == "SXDH" || pageCode == "BST" || pageCode == "MBT" || pageCode == "LWG" || pageCode == "ZHB" || pageCode == "RZJB" || pageCode == "LZBB" || pageCode == "DXB" || pageCode == "WFG")
            {
                needchange = true;
            }
            else if (pageCode == "QSHYD" || pageCode == "GHYYDL" || pageCode == "CXJMSHYD")
            {
                needchange = true;
            }
            else if (pageCode == "SPFJYSJ")
            {
                needchange = true;
            }
            else
            {
                needchange = false;
            }
            return needchange;
        }

        /// <summary>
        /// 生成近期数据的表格(价格上限，价格下限的表)
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="code"></param>
        /// <param name="queryCount"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public static string BuildCurrentDataTBody(string tableName, string filters, string orderBy, List<string> tableColumns, List<string> priceColumns, string pageCode)
        {
            //List<App_Codes.Prices> list = DataCapture.GetCurrentDatas(tableName, filters, orderBy, priceColumns);
            //return BuildCurrentDataOfUpDownTBody(list, tableColumns, pageCode);

            List<App_Codes.Prices> list = new List<App_Codes.Prices>();

            string _tablePrices = "";
            foreach (string s in priceColumns)
            {
                _tablePrices += s;
            }

            string cacheString = "__" + tableName + filters + orderBy + _tablePrices + App_Codes.GetCount.Count;



            object obj = CacheManager.GetCache(cacheString);
            if (obj != null)
            {
                list = obj as List<App_Codes.Prices>;
            }
            else
            {
                list = TableHtmlGeterate.DataCapture.GetCurrentDatas(tableName, filters, orderBy, priceColumns);

                CacheManager.AddCache(cacheString, list, App_Codes.GetCount.CacheTime * 30);
            }
            return BuildCurrentDataOfUpDownTBody(list, tableColumns, pageCode);
        }
    }
}